<?php

/**
 * This is the model class for table "tbl_invcategories".
 *
 * The followings are the available columns in table 'tbl_invcategories':
 * @property integer $id
 * @property string $categories
 * @property integer $status
 * @property string $image
 */
class Invcategories extends CActiveRecord
{
    public $file;
	/**
	 * Returns the static model of the specified AR class.
	 * @return Invcategories the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_invcategories';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('categories', 'required'),
			array('status', 'numerical', 'integerOnly'=>true),
			array('categories', 'length', 'max'=>75),
                        array('file','file','types'=>'jpg,png,jpeg,bmp,gif','maxSize'=>1000000,'allowEmpty'=>true),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, categories, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'categories' => 'Categories',
			'status' => 'Status',
                        'image'=>'Image'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('categories',$this->categories,true);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        public function beforeSave(){
            
            
            $this->file=CUploadedFile::getInstance($this, 'file');
            if($this->file!=NULL){
                    //made a random name base on timestamp
                $ext=getFileExt($this->file);
                $name=getFileBodyName($this->file);


                $this->image=$name.'_'.time().$ext;
               
                $this->file->saveAs('images/content/categories/'.$this->image,true);   
            }
            return parent::beforeSave();
        }
        public function getStatusList(){
            return array(
                '0'=>'Active',
                '-1'=>'Inactive',
                
            );
        }
        public function getStatusText(){
            return isset($this->statusList[$this->status])?$this->statusList[$this->status]:NULL;
        }
        
        public function getSubCategoryList($cat=NULL){
           
            $data=Yii::app()->db->createCommand('select * from tbl_invsubcategories where category=:id AND status=0')->bindValue('id', $this->id)->queryAll();
            if(count($data)>0){
                return $data;
            }
            return $data;
        }
        
        public function getSubCategoryString(){
            $rs='';
            foreach($this->getSubCategoryList() as $i=>$item){
                
                $rs.="{$item['subcategory']}";
                if($i< count($this->getSubCategoryList())-1){
                    $rs.=', ';
                }
            }
            return $rs;
        }
        
}